package com.hippy.erya.modular.erya;

import cn.stylefeng.roses.kernel.model.response.ResponseData;
import com.hippy.erya.base.auth.context.LoginContextHolder;
import com.hippy.erya.base.pojo.page.LayuiPageFactory;
import com.hippy.erya.base.pojo.page.LayuiPageInfo;
import com.hippy.erya.entity.DishDO;
import com.hippy.erya.entity.RawMaterialDictionaryDO;
import com.hippy.erya.impl.DishServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.websocket.server.PathParam;
import java.util.List;

@Slf4j
@Controller
@RequestMapping("/erya/dish")
public class DishController {

    @Autowired
    private DishServiceImpl dishService;

    @RequestMapping(value = "/dish")
    public String dish() {
        return "/erya/dish/dish_setting.html";
    }

    @RequestMapping(value = "/dishAdd")
    public String dishAdd() {
        return "/erya/dish/dish_add.html";
    }

    @RequestMapping(value = "/dishEdit")
    public String dishEdit() {
        return "/erya/dish/dish_edit.html";
    }

    @RequestMapping("/queryAll")
    @ResponseBody
    public LayuiPageInfo queryByPage(@PathParam("page") Long page, @PathParam("limit") int limit,
                                     @PathParam("dishCode") String dishCode,
                                     @PathParam("dishName") String dishName) {
        return LayuiPageFactory.createPageInfo(dishService.queryByPage(page, limit, dishName, dishCode));
    }

    @RequestMapping("/deleteData/{id}")
    @ResponseBody
    public ResponseData deleteData(@PathVariable("id") int id) {
        dishService.deleteById(id);
        return ResponseData.success();
    }

    @RequestMapping("/addData")
    @ResponseBody
    public ResponseData addData(DishDO dishDO) {
        // 添加同一个菜品名称校验
        if (!checkDishAddDO(dishDO)) {
            return ResponseData.error("该销售菜品已存在！");
        }
        String user = String.valueOf(LoginContextHolder.getContext().getUserId());
        dishDO.setUserCode(user);
        dishDO.setCreatedBy(user);
        dishDO.setUpdatedBy(user);
        dishService.save(dishDO);
        return ResponseData.success();
    }

    @RequestMapping("/editData")
    @ResponseBody
    public ResponseData editData(DishDO dishDO) {
        String user = String.valueOf(LoginContextHolder.getContext().getUserId());
        dishDO.setUpdatedBy(user);
        dishService.updateById(dishDO);
        return ResponseData.success();
    }


    @RequestMapping("/dishDetail/{id}")
    @ResponseBody
    public ResponseData editData(@PathVariable("id") int id) {
        return ResponseData.success(dishService.getById(id));
    }


    private boolean checkDishAddDO(DishDO dishDO) {
        List<DishDO> dishList = dishService.queryByDishName(dishDO.getDishName());
        if (!CollectionUtils.isEmpty(dishList)) {
            return false;
        }
        return true;
    }
}
